//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.Sanitize;
import tv.dyndns.kishibe.client.packet.PacketProblemData;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;

public class WidgetInput4Taku extends WidgetInputBase implements ClickHandler {
	private static final String STYLE_4TAKU = "gwt-Button-4taku";
	private static final String STYLE_4TAKU_IMAGE = "gwt-Button-4takuImage";
	private final Button buttonChoice[];
	private final WidgetAnswer widgetAnswer;
	private final PacketProblemData problemData;
	private final int letterType;

	public WidgetInput4Taku(WidgetAnswer answer, PacketProblemData problemData) {
		this.problemData = problemData;
		letterType = (problemData.type == Constant.TYPE_4TAKU) ? DIGIT : ALPHA;
		buttonChoice = new Button[problemData.choice.length];
		widgetAnswer = answer;

		if (problemData.imageChoice) {
			final Grid grid = new Grid(2, 2);
			final int numberOfChoice = problemData.getNumberOfChoice();
			final int letterType = (problemData.type == Constant.TYPE_4TAKU) ? DIGIT : ALPHA;

			for (int i = 0; i < numberOfChoice; ++i) {
				final String choice = problemData.randomizedChoice[i];
				final String html = getLetter(letterType, i) + "<img src='" + Sanitize.getInstance().uri(URL.encode(URL.decode(choice))) + "' width='160px' height='120px'/>";
				buttonChoice[i] = new Button(html, this);
				buttonChoice[i].setStyleName(STYLE_4TAKU_IMAGE);
				grid.setWidget(i / 2, i % 2, buttonChoice[i]);
			}
			add(grid);

		} else {
			final Grid grid = new Grid(problemData.choice.length, 2);
			final int numberOfChoice = problemData.getNumberOfChoice();
			final int letterType = (problemData.type == Constant.TYPE_4TAKU) ? DIGIT : ALPHA;

			for (int i = 0; i < numberOfChoice; ++i) {
				final Label label = new Label(getLetter(letterType, i));
				label.addStyleDependentName("buttonIndex");
				grid.setWidget(i, 0, label);

				final String choice = problemData.randomizedChoice[i];
				buttonChoice[i] = new Button(Sanitize.getInstance().htmlChoice(choice), this);
				buttonChoice[i].setStyleName(STYLE_4TAKU);
				grid.setWidget(i, 1, buttonChoice[i]);
			}

			add(grid);
		}

		add(new HTML("4つの選択肢から正しい答えを選んでください"));
	}

	public void enable(boolean b) {
		for (int i = 0; i < buttonChoice.length; ++i) {
			buttonChoice[i].setEnabled(b);
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		final Object sender = event.getSource();
		int index = 0;
		for (; index < 4; ++index) {
			if (sender == buttonChoice[index]) {
				break;
			}
		}
		final String letter = getLetter(letterType, index);
		widgetAnswer.setText(letter);
		sendAnswer(problemData.randomizedChoice[index]);
	}

	@Override
	public void hideAnswer() {
		widgetAnswer.setText(ANSWERED);
	}
}
